//package com.origin.filestorage.operation.upload.product;
//
//import com.origin.filestorage.operation.upload.FileUploader;
//import com.origin.filestorage.operation.upload.domain.FileSliceUpload;
//import com.origin.filestorage.operation.upload.domain.FileUploadResult;
//import com.origin.filestorage.operation.upload.request.QiwenMultipartFile;
//import com.origin.filestorage.config.MinioConfig;
//import com.origin.filestorage.enums.StorageTypeEnum;
//import com.origin.filestorage.enums.FileUploadStateEnum;
//import com.origin.filestorage.exception.operation.UploadException;
//import com.origin.filestorage.util.RedisUtil;
//import com.origin.filestorage.util.UFOPUtils;
//import io.minio.*;
//import io.minio.errors.*;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.IOUtils;
//
//import javax.annotation.Resource;
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.*;
//import java.security.InvalidKeyException;
//import java.security.NoSuchAlgorithmException;
//
//@Slf4j
//public class MinioUploader extends FileUploader {
//
//    private MinioConfig minioConfig;
//
//    @Resource
//    RedisUtil redisUtil;
//
//    public MinioUploader(){
//
//    }
//
//    public MinioUploader(MinioConfig minioConfig){
//        this.minioConfig = minioConfig;
//    }
//
//    @Override
//    public void cancelUpload(FileSliceUpload uploadFile) {
//    }
//
//    @Override
//    protected FileUploadResult organizationalResults(QiwenMultipartFile qiwenMultipartFile, FileSliceUpload uploadFile) {
//        return null;
//    }
//
//    @Override
//    protected FileUploadResult doUpload(QiwenMultipartFile qiwenMultipartFile, FileSliceUpload uploadFile) {
//        FileUploadResult uploadFileResult = new FileUploadResult();
//        try {
//            qiwenMultipartFile.getFileUrl(uploadFile.getIdentifier());
//            String fileUrl = UFOPUtils.getUploadFileUrl(uploadFile.getIdentifier(), qiwenMultipartFile.getExtendName());
//
//            File tempFile =  UFOPUtils.getTempFile(fileUrl);
//            File processFile = UFOPUtils.getProcessFile(fileUrl);
//
//            byte[] fileData = qiwenMultipartFile.getUploadBytes();
//
//            writeByteDataToFile(fileData, tempFile, uploadFile);
//
//            //判断是否完成文件的传输并进行校验与重命名
//            boolean isComplete = checkUploadStatus(uploadFile, processFile);
//            uploadFileResult.setFileUrl(fileUrl);
//            uploadFileResult.setFileName(qiwenMultipartFile.getFileName());
//            uploadFileResult.setExtendName(qiwenMultipartFile.getExtendName());
//            uploadFileResult.setFileSize(uploadFile.getTotalSize());
//            uploadFileResult.setStorageType(StorageTypeEnum.MINIO);
//
//            if (uploadFile.getTotalChunks() == 1) {
//                uploadFileResult.setFileSize(qiwenMultipartFile.getSize());
//            }
//            uploadFileResult.setIdentifier(uploadFile.getIdentifier());
//            if (isComplete) {
//
//                minioUpload(fileUrl, tempFile, uploadFile);
//                uploadFileResult.setFileUrl(fileUrl);
//                tempFile.delete();
//
//                if (UFOPUtils.isImageFile(uploadFileResult.getExtendName())) {
//                    InputStream inputStream = null;
//                    try {
//                        MinioClient minioClient =
//                                MinioClient.builder().endpoint(minioConfig.getEndpoint())
//                                        .credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
//
//                        inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(uploadFileResult.getFileUrl()).build());
//
//                        BufferedImage src  = ImageIO.read(inputStream);
//                        uploadFileResult.setBufferedImage(src);
//                    } catch (IOException | InternalException | XmlParserException | InvalidResponseException | InvalidKeyException | NoSuchAlgorithmException | ErrorResponseException | InsufficientDataException | ServerException e) {
//                        e.printStackTrace();
//                    } finally {
//                        IOUtils.closeQuietly(inputStream);
//                    }
//
//                }
//
//                uploadFileResult.setStatus(FileUploadStateEnum.SUCCESS);
//            } else {
//                uploadFileResult.setStatus(FileUploadStateEnum.UNCOMPLETE);
//            }
//        } catch (IOException e) {
//            throw new UploadException(e);
//        }
//
//
//        return uploadFileResult;
//    }
//
//
//    private void minioUpload(String fileUrl, File file,  FileSliceUpload uploadFile) {
//        InputStream inputStream = null;
//        try {
//            MinioClient minioClient =
//                    MinioClient.builder().endpoint(minioConfig.getEndpoint())
//                            .credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
//            // 检查存储桶是否已经存在
//            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getBucketName()).build());
//            if(!isExist) {
//                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioConfig.getBucketName()).build());
//            }
//
//            inputStream = new FileInputStream(file);
//            minioClient.putObject(
//                    PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileUrl).stream(
//                                    inputStream, uploadFile.getTotalSize(), 1024 * 1024 * 5)
////                            .contentType("video/mp4")
//                            .build());
//        } catch (MinioException | InvalidKeyException | NoSuchAlgorithmException | IOException e) {
//            e.printStackTrace();
//        } finally {
//            IOUtils.closeQuietly(inputStream);
//        }
//
//    }
//
//
//}
